﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Text;
using System.Data;

namespace Catering.Catering_Page.Caipin.Caipin_jcsz
{
    public partial class shipu_clone03 : UserIsNull
    {
        public int m_CID = 0;               //GroupCatering表ID
        public int m_ClassID = 0;
        public int m_RecipesID = 0;
        public int m_IsGroupCatering = 0;   //0：个人配餐，1：群体配餐
        public int m_CheckDayCount = 8;  //默认为7天制

        protected void Page_Load(object sender, EventArgs e)
        {
            //获取参数
            m_CID = GetInt("cid", 0);
            m_ClassID = GetInt("classid", 0);
            m_IsGroupCatering = GetInt("IsGroupCatering", 0);
            m_RecipesID = GetInt("recipesid", 0);
            if (m_CID == 0 || m_ClassID == 0 || m_RecipesID == 0) return;
            if (m_IsGroupCatering == 1)
            {
                this.HidIsQun.Value = "true";
                this.HidClassID.Value = m_ClassID.ToString();
            }
            Catering_Model.E_RecipesWeek rw = new Catering_Rules.T_RecipesWeek().GetModel(" RecipesID=" + m_RecipesID + " and Number=0");
            if (rw != null) m_CheckDayCount = (rw.endDate - rw.startDate).Days + 2;
        }

        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="OptionName"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public int GetInt(string OptionName, int DefaultValue)
        {
            if (Request.QueryString[OptionName] == null) return DefaultValue;
            string tempstr = Request.QueryString[OptionName].ToString();
            Int32.TryParse(tempstr, out DefaultValue);
            return DefaultValue;
        }

        protected void Next_But_Click(object sender, EventArgs e)
        {
            string strdatetime = this.hf_datelist.Value;//选择的时间段
            string[] datetimes = strdatetime.TrimEnd(',').Split(',');//时间周期

            DateTime starttime = Convert.ToDateTime(datetimes[0].Split('|')[0]);
            DateTime endtime = Convert.ToDateTime(datetimes[datetimes.Length - 1].Split('|')[1]);

            if (m_IsGroupCatering == 1)//群体配餐
            {
                Catering_Model.E_Recipes md = new Catering_Rules.T_Recipes().GetModel(m_RecipesID);
                md.CateringName = new Catering_Rules.GroupCatering().GetModel(md.GroupID).GCName;
                md.StateDate = starttime;
                md.StopDate = endtime;
                md.Number = new Catering_Rules.CateringGroupInfo().GetSumRSByGroupID(m_CID.ToString());
                new Catering_Rules.T_Recipes().Update(md);
                this.SaveRecipesWeek(datetimes, md.id);
                //食谱详细表
                List<Catering_Model.E_RecipeInformation> li_ri = new Catering_Rules.T_RecipeInformation().GetModelList(" MID=" + md.id);
                if (li_ri != null && li_ri.Count() > 0)
                {
                    foreach (Catering_Model.E_RecipeInformation item in li_ri)
                    {
                        Catering_Model.E_RecipesWeek rw = new Catering_Rules.T_RecipesWeek().GetModel(" RecipesID=" + item.MID + " and Number=" + item.temp1);
                        if (rw != null)
                        {
                            item.Date = rw.startDate.AddDays((item.Week == 0 ? 7 : item.Week) - 1);
                            new Catering_Rules.T_RecipeInformation().Update(item);
                        }
                        else
                        {
                            new Catering_Rules.T_RecipeInformation().Delete(item.id);
                        }
                    }
                }
                Response.Redirect("shipu_clone05.aspx?cid=" + md.id + "&IsGroupCatering=true&IsNew=false");
            }
            else //个人配餐
            {
                Catering_Model.PersonalService md_geren = new Catering_Rules.PersonalService().GetModel(m_CID);
                Catering_Model.E_Recipes md = new Catering_Rules.T_Recipes().GetModel(m_RecipesID);
                md.CateringName = md_geren.CateringName;
                md.PID = md_geren.id;
                md.StateDate = starttime;
                md.StopDate = endtime;
                md.Number = 1;
                new Catering_Rules.T_Recipes().Update(md);
                this.SaveRecipesWeek(datetimes, md.id);
                Response.Redirect("../shipu_left.aspx?cid=" + md.id);
            }
        }

        //保存辅助周期
        public void SaveRecipesWeek(string[] datetimes, int RecipesID)
        {
            //删除周期
            new Catering_Rules.T_RecipesWeek().DeleteListBywhere(" RecipesID=" + RecipesID + " ");
            for (int i = 0; i < datetimes.Length; i++)
            {
                Catering_Model.E_RecipesWeek md_week = new Catering_Model.E_RecipesWeek();
                md_week.startDate = Convert.ToDateTime(datetimes[i].Split('|')[0]);
                md_week.endDate = Convert.ToDateTime(datetimes[i].Split('|')[1]);
                md_week.Number = i;
                md_week.RecipesID = RecipesID;
                md_week.temp1 = "第" + (i + 1).ToString() + "周";
                md_week.id = new Catering_Rules.T_RecipesWeek().GetMaxId();
                new Catering_Rules.T_RecipesWeek().Add(md_week);
            }
        }

        //验证日期
        [WebMethod]
        public static string GetDatatime(string starttime, string endtime, string ClassID, string StrRecipesID)
        {
            int tempid = 0;
            StringBuilder sb = new StringBuilder();
            DateTime Start = Convert.ToDateTime(starttime);
            DateTime End = Convert.ToDateTime(endtime);
            Int32.TryParse(StrRecipesID, out tempid);
            DataTable dt_Week = new Catering_Rules.T_RecipesWeek().GetListByClassID(Convert.ToInt32(ClassID), Start, End).Tables[0];
            Catering_Model.E_Recipes md = new Catering_Rules.T_Recipes().GetModel(tempid);


            if (dt_Week != null && dt_Week.Rows.Count > 0)
            {
                foreach (DataRow row in dt_Week.Rows)
                {
                    string Stime = Convert.ToDateTime(row["startDate"].ToString()).ToString("yyyy-MM-dd");
                    string Etime = Convert.ToDateTime(row["endDate"].ToString()).ToString("yyyy-MM-dd");
                    if (md != null && Stime != md.StateDate.ToString("yyyy-MM-dd") && Etime != md.StopDate.ToString("yyyy-MM-dd"))
                    {
                        sb.Append(Stime + "|" + Etime + ",");
                    }
                }
            }
            return sb.ToString().TrimEnd(',');
        }

        public string GetCheck(int i)
        {
            if (this.m_CheckDayCount == i)
                return "checked='checked'";
            else return "";
        }
    }
}